<!-- <script>
    // 什么是迭代器
    const names = ["码路", "漫漫", "前端"];

    // 给数组names创建一个迭代器(迭代器: names的迭代器)
    let index = 0

    // 迭代器就是一个对象，要求这个对象必须有一个next方法
    // Iterator 是迭代器  就可以把一个容器中的元素迭代出来
    const namesIterator = {
        next() {
            if (index < names.length) {
                return { done: false, value: names[index++] }
            } else {
                return { done: true, value: undefined }
            }
        }
    };
    // 调用next返回一个对象，done表示是否迭代完毕，value表示迭代出来的数据
    console.log(namesIterator.next()); // { done:false,value:"码路" }
    console.log(namesIterator.next()); // { done:false,value:"漫漫" }
    console.log(namesIterator.next()); // { done:false,value:"前端" }
    console.log(namesIterator.next()); // { done:false,value:undefined }
</script> -->








<!-- <script>
    const nums = [100, 200, 300, 400, 500]

    let index = 0;
    let numsIterator = {
        next() {
            if (index < nums.length) {
                return { done: false, value: nums[index++] }
            } else {
                return { done: true, value: undefined }
            }
        }
    }

    console.log(numsIterator.next()); // { done: false, value: 100 }
    console.log(numsIterator.next()); // { done: false, value: 200 }
    console.log(numsIterator.next()); // { done: false, value: 300 }
    console.log(numsIterator.next()); // { done: false, value: 400 }
    console.log(numsIterator.next()); // { done: false, value: 500 }
    console.log(numsIterator.next()); // { done: false, value: undefined }
</script> -->













<script>
    // 封装一个函数，返回一个数组迭代器(Iterator)
    const nums = [100, 200, 300, 400, 500]
    const names = ["码路", "漫漫", "前端"];

    function createArrayIterator(arr) {
        let index = 0;
        return {
            next() {
                if (index < arr.length) {
                    return { done: false, value: arr[index++] }
                } else {
                    return { done: true, value: undefined }
                }
            }
        }
    }
    let namesIterator = createArrayIterator(names);
    console.log(namesIterator.next());
    console.log(namesIterator.next());
    console.log(namesIterator.next());

    let numsIterator = createArrayIterator(nums);
    console.log(numsIterator.next());
    console.log(numsIterator.next());
    console.log(numsIterator.next());

</script>